package cn.wolfcode.controller;

import cn.wolfcode.domain.Product;
import cn.wolfcode.feign.IProductFeignApi;
import cn.wolfcode.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: springcloud推崇的不适用controller进行对外提供接口服务，使用feign客户端方式
 * 其实本质还是controller，换一种写法而已。
 * @Author: 伍家钰
 * @Date: 2020/8/28 20:44
 * @Version: 1.0
 */
@RestController
@Slf4j
public class ProductFeignClient implements IProductFeignApi {

    @Autowired
    private IProductService productService;

    @Value("${server.port}")
    private String port;

    @Override
    public Product get(Long id) {
        System.out.println("ProductFeignClient..get...");
        Product product = productService.get(id);
        // 模拟网络延时3秒
       /* try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        // 模拟功能异常时服务挂了,是否执行降级方法
        //int i = 1 / 0;

        //想让order-server服务调用product集群时,知道调用哪一个服务(8080?8081?)
        //product.setName(product.getName() + "-" + port);
        //防止端口重复拼接
        Product pp = new Product();
        BeanUtils.copyProperties(product, pp);
        pp.setName(product.getName() + "-" + port);
        //记录调用日志
        log.info("ProductController.get....");
        return pp;
    }
}
